import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Calendar;
/**	
 * This class ...
 */

public class Controller
{

   public static boolean checkDriverID(String driverNo)
   {
       if(DriverInfo.findDriver(driverNo)!=0)return true;
       else return false;
   }//

   public static String checkReq(Date start, Date end,int driverId)
   {
     long diff = (end.getTime()-start.getTime())/(1000 * 60 * 60 * 24) +1;
     if(diff<0)
       return "startDate >= endDate";
     Date today_date = (Date)(Calendar.getInstance().getTime().clone());
     if(start.before(today_date))
       return "You can not request holiday after today!";
    //  if(start.getDay()<0 || start.getDay()>31) return "invalid date in a month"
      if(diff+DriverInfo.getHolidaysTaken(driverId) > 25)return "Holiday exceeds 25 days limit.";
 
      int[] driverIDs = DriverInfo.getDrivers();
      end.setDate(end.getDate()+1);
      //check if other days are free or not
      Date index = (Date)start.clone();
      while (!index.equals(end) )
      {
        if(!DriverInfo.isAvailable(driverId,index)){//System.out.println("\n Bla"+index); 
        return "Date not available. You have requested that date!"; }
      //check only 10 holidays/day
        int count =0;
        for (int i=0; i<driverIDs.length; i++)
        {
          if(count==10 && start.getDay() !=7)
	    return "Maximum allowed number of " + index.getDate() + "/" + 
		 (index.getMonth()+1) + "/" + (index.getYear()+1900) +
		" has been reached. holiday denied";
          if( !DriverInfo.isAvailable(driverIDs[i],index))count++;
        }
        
 	index.setDate(index.getDate()+1);
      }

      
     // set all the days
      while (!start.equals(end))
      {
          DriverInfo.setAvailable(driverId, start, false) ;
          start.setDate(start.getDate()+1);
      }//while
      DriverInfo.setHolidaysTaken(driverId, (int)diff+DriverInfo.getHolidaysTaken(driverId));
      return ("Holiday approved.Holiday no: "+ DriverInfo.getHolidaysTaken(driverId));
   }//checkReq
    
   
}
